package com.example.systemtimeclient.service;


import com.example.systemtimeclient.util.OSinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TransferServiceImpl implements TransferService {


    @Value("${systemTimeService.url}")
    String url;
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    @Scheduled(cron = "${systemTimeService.cycleTimeCron}")// 0 0 0 * * ?  每天00:00:00触发
    public void alarmEventDay() {
        RestTemplate restTemplate = restTemplateBuilder.build();
        //getForObject方法，获取响应体，将其转换为第二个参数指定的类型
        Time time = restTemplate.postForObject(url, null, Time.class);
        if(time==null){
            Exception exception = new Exception("时间未获取");
            exception.printStackTrace();
        }
        try {
            switch (OSinfo.getOSname()) {
                case Windows:
                    Runtime.getRuntime().exec("cmd /c date "+time.getDate());//Windows 系统
                    Runtime.getRuntime().exec("cmd /c time "+time.getTime());//Windows 系统
                    System.out.println("修改windows时间 cmd /c time "+time.getTime());
                    break;
                case Linux:
                    String [] cmd={"date","-s",time.getDate()+" " +time.getTime()};
                    Process proc =Runtime.getRuntime().exec(cmd);

                      if(proc.waitFor()==0){
                          System.out.println("时间修改成功，当前时间：");
                          readProcessOutput(proc);
                      }else {
                          System.out.println("error：");
                          readProcessOutput(proc);
                      }
                    break;
                default:
                    System.out.println("不支持该"+OSinfo.getOSname()+"系统");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 打印进程输出
     *
     * @param process 进程
     */
    private static void readProcessOutput(final Process process) {
        // 将进程的正常输出在 System.out 中打印，进程的错误输出在 System.err 中打印
        read(process.getInputStream(), System.out);
        read(process.getErrorStream(), System.err);
    }

    private static void read(InputStream inputStream, PrintStream out) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    int commandWaitTime(Process process){
        int waitTime = 0;
        try {
            while (process.waitFor()!=0){
                wait(10);
                waitTime+=10;
            }
            readProcessOutput(process);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return waitTime;
    }


}

class Time {
    String time;
    String date;

    Time() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        this.time = df.format(new Date());
        SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss");
        this.date = df1.format(new Date());
    }

    public String getTime() {
        return time;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getDate() {
        return date;
    }
}